package com.cn.action;

import com.cn.Intface.PhoneYz;
import com.cn.entity.UserEntity;
import com.cn.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


public class UpdateUserAction implements ActionInterface {
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        String uphone = request.getParameter("phone");
        String upassword = request.getParameter("password");
        UserEntity userEntity = new UserEntity();
        userEntity.setPassword(upassword);
        userEntity.setPhone(uphone);
        UserService us = new UserService();
        UserEntity userEntity1 = new UserEntity();
        String smscode = request.getParameter("sms");
        PhoneYz phoneYz = (PhoneYz) request.getSession().getAttribute("PhoneYz");
        request.removeAttribute("update");
        request.removeAttribute("updatecg");
        System.out.println("aaaa");
        System.out.println(smscode);
        if (smscode.equals(phoneYz.getUserRandom())) {
            System.out.println("ssss");
            userEntity1 = us.queryOne(userEntity, "zc");
            if (userEntity1 != null) {
                us.updateUser(userEntity, "updatephone");
                request.setAttribute("updatecg", "修改密码成功");
                return "login.jsp";
            } else {
                request.setAttribute("update", "修改失败,该用户不存在！！！");
                return "login.jsp";
            }
        } else {
            request.setAttribute("update", "验证码输入错误！！！");
            return "login.jsp";
        }
    }
}
